import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QObject, pyqtSlot, QUrl
from PyQt5.QtWebChannel import QWebChannel

class NativeApi(QObject):
    @pyqtSlot(str, int, result=str)
    def read_serial_port(self, port_name, baud_rate):
        # 实际串口读取逻辑（需安装pyserial）
        import serial
        with serial.Serial(port_name, baud_rate, timeout=1) as ser:
            return ser.readline().decode('utf-8')

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.view = QWebEngineView(self)
        self.channel = QWebChannel()
        self.native_api = NativeApi()
        self.channel.registerObject("nativeApi", self.native_api)
        self.view.page().setWebChannel(self.channel)
        self.view.load(QUrl.fromLocalFile("D:\LXH_prj\\2_通用网页上位机设想\index.html"))  # 指向本地HTML路径
        self.view.setGeometry(0, 0, 800, 600)
        self.setGeometry(100, 100, 800, 600)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
    